/**
 * 
 */
package cn.crm.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import cn.crm.bean.Right;
import cn.crm.bean.RightMaping;
import cn.crm.bean.User;
import cn.crm.dao.RightMapingDao;
import cn.crm.service.UserService;

/**
 * @author Vi1___000
 *
 */
@Repository("rightMapingDao")
public class RightMapingDaoImpl extends HibernateDaoSupport implements RightMapingDao {

	@Override
	public List<RightMaping> getByUser(User user) {
		// TODO Auto-generated method stub
		return super.getHibernateTemplate().find("from RightMaping where user.usCid='"+user.getUsCid()+"' and isok=1");
	}

	@Override
	public boolean add(RightMaping maping) {
		// TODO Auto-generated method stub
		
		try {
			super.getHibernateTemplate().save(maping);
			return true;
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return false;
	}

	@Override
	public boolean del(RightMaping maping) {
		// TODO Auto-generated method stub
		try {
			super.getHibernateTemplate().delete(maping);
			return true;
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return false;
	}

	@Override
	public boolean update(RightMaping maping) {
		// TODO Auto-generated method stub
		try {
			super.getHibernateTemplate().update(maping);
			return true;
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return false;
	}

	@Override
	public RightMaping getByID(Integer id) {
		// TODO Auto-generated method stub
		return super.getHibernateTemplate().get(RightMaping.class, id);
	}

	@Override
	public boolean delByUserRight(Integer uid, String rid) {
		// TODO Auto-generated method stub
		Session session = super.getSession();
		String sql = "delete rightmaping where usid="+uid+" and rid='"+rid+"'";
		Query query = session.createSQLQuery(sql);
		if(query.executeUpdate()>0){
			return true;
		}
		return false;
	}


}
